﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using InstrumentCheck.Module;
using InstrumentCheck.SQLHelper;

namespace InstrumentCheck.DAL
{
    public class OriginalBox_dal
    {
        public OriginalBox_dal()
        { }

        public List<ApplyForm> GetOriginalBox(string strUserID)
        {
            string sql = "select correctID,Number,dbo.f_GetEmpNameByNo(Applicant) Applicant,Create_date,";
            sql += "Fas_No,dbo.f_GetItemName(Fas_NO,Fas_Type) ChiName,dbo.f_GetItemSpec(Fas_NO,Fas_Type) Spec,";
            sql += "dbo.f_GetItemModelNO(Fas_NO,Fas_Type) ModelNO,FormStatus FROM Correct ";
            sql += "WHERE FormStatus <> 0 and Creater='" + strUserID + "'";
            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter();
            SQLHelper.SQLHelper.Fill(da, ds, sql, CommandType.Text, null);

            List<ApplyForm> list = new List<ApplyForm>();
            foreach (DataRow row in ds.Tables[0].Rows)
                list.Add(GenerateFormFromRow(row));
            return list;
        }

        public ApplyForm GenerateFormFromRow(DataRow row)
        {
            ApplyForm form = new ApplyForm();
            Item item = new Item();
            User applicant = new User();
            form.FormID = int.Parse(row["CorrectID"].ToString());
            form.FormNO = int.Parse(row["Number"].ToString());
            applicant.EmpChineseName = row["Applicant"].ToString();
            item.AssetNO = row["Fas_NO"].ToString();
            item.ChiName = row["ChiName"].ToString();
            item.Spec = row["Spec"].ToString();
            item.ModelNO = row["ModelNO"].ToString();
            form.Applicant = applicant;
            form.Item = item;
            form.FormStatus = (FormStatus)int.Parse(row["FormStatus"].ToString());
            form.ApplyDate = Convert.ToDateTime(row["Create_date"].ToString());

            return form;
        }
    }
}
